Skip to content

Conversation

leolca
Copy link
Contributor

@leolca leolca commented Sep 25, 2025

I've made an implementation of the dither function, compatible with Matlab implementation (https://www.mathworks.com/help/matlab/ref/dither.html). In Matlab it seems not to be a part of Image package, so I guess it would also fit directly into octave core.

Copy link
Member

@mmuetzel mmuetzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your contribution.

Sorry it took so long for someone to review your PR. This is just a read-only mirror of the upstream Mercurial repository. (But we try to port PR from here to the upstream repository if possible.)

I haven't checked the new function yet. But I left a few comments about the coding style that is used in Octave.

For the time being, I only took a look at the function documentation and had a very quick look at the function bodies (mostly for style).

I didn't bother to leave a comment at each and every line where the style should be adapted. But I tried to describe the coding style at some examples.

print_usage;
endif
if ndims (RGB) == 2
RGB = cat (3, RGB, RGB, RGB); % Duplicate grayscale to RGB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For end-of line comments, use two spaces and a single # as the comment character.

endif
endif
if ndims (RGB) != 3 || size (RGB, 3) != 3
error('dither: RGB must be an m x n x 3 array.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a whitespace between function name and opening parenthesis.

if ndims (RGB) != 3 || size (RGB, 3) != 3
error('dither: RGB must be an m x n x 3 array.');
end
if !ismatrix (map) || size (map, 2) != 3 || min (map(:)) < 0 || max (map(:)) > 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a space after the negation operator (i.e., ! ismatrix (map)).
Use parenthesis around the condition in if statements.

endif
Qe = min (Qe, 16); % Cap Qe to avoid excessive precision

% Scale RGB and map to [0, 1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ## to introduce comments that span the entire line.

## @deftypefn {Function File} {@var{X} = } dither (@var{RGB}, @var{map})
## @deftypefnx {Function File} {@var{X} = } dither (@var{RGB}, @var{map}, @var{Qm}, @var{Qe})
## @deftypefnx {Function File} {@var{BW} = } dither (@var{I})
##
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a one-line description of the function if possible.

Comment on lines 22 to 24
## @code{@var{X} = dither (@var{RGB},@var{map})} creates an indexed image
## approximation, using the color provided in the colormap, and uses dithering
## to increase apparent color resolution. Floyd-Steinberg error filter is used:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is pretty long. Consider splitting it into two sentences.

## to increase apparent color resolution. Floyd-Steinberg error filter is used:
## [ x 7]
## [3 5 1] / 16
## It used a raster scan and no weight renormalization at boundaries.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo? "used" -> "uses"

## It used a raster scan and no weight renormalization at boundaries.
## The default values are used: @var{Qm}=5, and @var{Qe}=8.
##
## Inputs:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually don't use "section headings" like this. Also note, that texinfo will convert this and the following four lines into one single long line.

## error calculations in the Floyd-Steinberg error diffusion algorithm.
## It controls the precision of the error values that are calculated and
## propagated during dithering. If @var{Qe} < @var{Qm}, the error diffusion
## process may lose precision, therefore dithering cannot be performed, and the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full-stop after "precision".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants